/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductAddView.java
 *
 * Created on 12/10/2009, 11:21:20 PM
 */

package javaclientcp3110.View.Product;

import common.Objects.Component;
import java.awt.event.ItemEvent;
import java.util.Vector;
import javaclientcp3110.Model.Product.ProductAddModel;
import common.UCM.UC2AL;
import java.awt.event.ItemListener;
import javaclientcp3110.UCM.UCAddProduct;
import javaclientcp3110.View.Component.ComponentListView;
import javax.swing.JOptionPane;

/**
 *
 * @author Slasher
 */
public class ProductAddView extends javax.swing.JPanel {
    private ProductAddModel model;
    /** Creates new form ProductAddView */
    public ProductAddView() {
        this(new ProductAddModel());
    }

    public ProductAddView(ProductAddModel model) {
        this.model = model;
        initComponents();
        productComponentList.createList();
        for (ComponentListView clv : productComponentList.getComponentView()) {
            clv.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    updateBasePrice();
                }
            });
        }
        updateBasePrice();
        prodAddButton.addActionListener(new UC2AL(new UCAddProduct(this, model)));
    }
    private void updateBasePrice() {
        double basePrice = 0.0;
        for (ComponentListView clv : productComponentList.getComponentView()) {
            Component c = (Component) clv.getSelectedItem();
            basePrice += c.getPrice();
        }
        prodBasePriceTextField.setText(String.valueOf(basePrice));
    }
    public String getProdName() {return prodNameTextField.getText();}
    public String getProdDescription() {return prodDescriptionTextField.getText();}
    public String getProdPrice() {return prodRetailPriceTextField.getText();}
    public Vector<Component> getProdComponents() {
        Vector<Component> components = new Vector<Component>();
        for (ComponentListView clv : productComponentList.getComponentView()){
            components.add((Component)clv.getSelectedItem());
        }
        return components;
    }
    public void clearFields() {
        prodNameTextField.setText("");
        prodDescriptionTextField.setText("");
        prodRetailPriceTextField.setText("");
    }
    public void errorMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Error",
            JOptionPane.ERROR_MESSAGE);
    }
    public void successMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Success!",
            JOptionPane.INFORMATION_MESSAGE);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        productComponentList = new javaclientcp3110.View.Product.ComponentsListedViaCatagorysView();
        prodNameLabel = new javax.swing.JLabel();
        prodNameTextField = new javax.swing.JTextField();
        prodDescriptionLabel = new javax.swing.JLabel();
        prodDescriptionTextField = new javax.swing.JTextField();
        prodBasePriceLabel = new javax.swing.JLabel();
        prodBasePriceTextField = new javax.swing.JTextField();
        prodRetailPriceLabel = new javax.swing.JLabel();
        prodRetailPriceTextField = new javax.swing.JTextField();
        titleLabel = new javax.swing.JLabel();
        prodAddButton = new javax.swing.JButton();

        javax.swing.GroupLayout productComponentListLayout = new javax.swing.GroupLayout(productComponentList);
        productComponentList.setLayout(productComponentListLayout);
        productComponentListLayout.setHorizontalGroup(
            productComponentListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 226, Short.MAX_VALUE)
        );
        productComponentListLayout.setVerticalGroup(
            productComponentListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 87, Short.MAX_VALUE)
        );

        prodNameLabel.setText("Name:");

        prodNameTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodDescriptionLabel.setText("Description:");

        prodDescriptionTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodBasePriceLabel.setText("Base Price $");

        prodBasePriceTextField.setEditable(false);
        prodBasePriceTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodRetailPriceLabel.setText("Retail Price $");

        prodRetailPriceTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        titleLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        titleLabel.setText("Enter the details of the new Product");

        prodAddButton.setText("Add Product");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(prodAddButton))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(prodBasePriceLabel)
                                .addComponent(prodRetailPriceLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(prodRetailPriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                .addComponent(prodBasePriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)))
                        .addComponent(productComponentList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(prodDescriptionLabel)
                                .addComponent(prodNameLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(prodDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                .addComponent(prodNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))))
                .addGap(88, 88, 88))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodDescriptionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(productComponentList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodBasePriceLabel)
                    .addComponent(prodBasePriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodRetailPriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodRetailPriceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(prodAddButton)
                .addContainerGap(46, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton prodAddButton;
    private javax.swing.JLabel prodBasePriceLabel;
    private javax.swing.JTextField prodBasePriceTextField;
    private javax.swing.JLabel prodDescriptionLabel;
    private javax.swing.JTextField prodDescriptionTextField;
    private javax.swing.JLabel prodNameLabel;
    private javax.swing.JTextField prodNameTextField;
    private javax.swing.JLabel prodRetailPriceLabel;
    private javax.swing.JTextField prodRetailPriceTextField;
    private javaclientcp3110.View.Product.ComponentsListedViaCatagorysView productComponentList;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

}
